package cn.com.hfuu;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义过滤器
 * 过滤直接访问JSP、HTML页面
 * @author git
 *
 */
public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain filchain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest request=(HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) res;

		String contextpath=request.getContextPath();
		HttpSession s=request.getSession(false);
		/**
		 * 是否已登陆
		 */
		if(s==null||s.getAttribute("user")==null){
			
			response.sendRedirect(contextpath+"/login/home");
		}else{
			System.out.println(s.getAttribute("user"));
			filchain.doFilter(request, response);
		}
	}

	@Override
	public void init(FilterConfig fil) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
